package com.wsc.crawler.postcrawling;


/*
 *  this class is called by filter class to post URLs crawled by each thread
 *  this class generates an xml file with the urls crawled and sent to the frontier.
 */
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.wsc.crawler.CrawlerOperations;
import com.wsc.crawler.WSCrawler;
/**
 * This is a sample application that demonstrates
 * how to use the Jakarta HttpClient API.
 *
 * This application sends an XML document
 * to a remote web server using HTTP POST
 *
 * @author Sean C. Sullivan
 * @author Ortwin Glück
 * @author Oleg Kalnichevski
 */
public class PostURLs {

	static Logger log=Logger.getLogger(WSCrawler.class.getName());
	
 /*
  * postXML method takes xmldata(Parsed URLs) as a string, and a HttpHost which is set with frontier IP & port
  * on which frontier Server is up.
  * it checks the connectivity to frontier and, if it is ok then sends a POST request to Frontier and sets xmldata
  * as POST request Body.
  * 
  *  
  */
	public boolean postXML(String xmlData, HttpHost host) throws ClientProtocolException, IOException {

    	log.debug("postXML() is called.");
    
    	HttpHost s=host;
         
    	// construct a URL with "operation" parameter to intimate that the data is posting to frontier Server.
    	
    	String uri=s.toURI()+CrawlerOperations.P_CONTEXT_PATH+"?operation="+CrawlerOperations.O_CRAWLER_POST;
    	log.debug("URI to post URLs parsed from html to frontier Server is :"+uri);
        HttpPost post = new HttpPost(uri);
 
        System.out.println(post.getURI());
        // Request content will be retrieved directly
        // from the input stream
        // Per default, the request content needs to be buffered
        // in order to determine its length.
        // Request body buffering can be avoided when
        // content length is explicitly specified
        @SuppressWarnings("deprecation")
        /*
         * set XML data here 
         */
		HttpEntity entity=new StringEntity(xmlData,HTTP.UTF_8);
        // set xml data in post request
        post.setEntity(entity);
         
         post.addHeader("Content-type","application/xml");
        // Get HTTP client
        HttpClient httpclient = new DefaultHttpClient(); 

        // Execute request
        try {

            HttpResponse response = httpclient.execute(post);//.getStatusLine().getStatusCode();

            // Display status code
            log.debug("Response status code from frontier after POST URLs as XML is: " + response.getStatusLine().getStatusCode());
            log.debug("Response body from frontier after POST URLs as XML is "+EntityUtils.toString(response.getEntity()));
 
        } finally {
            // Release current connection to the connection pool 
            // once you are done
        	log.debug("Releasing Post connetion after posting URLs");
            post.releaseConnection();
        }
        return true;
    }
    
    
}